package littlewhisker.application {
	import littlewhisker.communication.RequestEvent;
	import littlewhisker.managers.LoopManager;
	import littlewhisker.managers.InitLoadManager;
	import littlewhisker.managers.ConfigManager;

	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.display.StageDisplayState;

	import littlewhisker.communication.EventBroadcaster;

	import flash.events.Event;

	import littlewhisker.globals.constants.eventtypes.LWEventTypes;
	
	import flash.display.Sprite;

	/**
	 * @author Philip
	 */
	public class AbstractApplication extends Sprite {
		
		private var _fullscreen:Boolean = false;
		private var _scalable:Boolean = true;
		private var _debug:Boolean = false;
	
		private var settings:XMLList;
		
		public function get debug() : Boolean {
			return _debug;
		}
		
		public function set debug(debug : Boolean) : void {
			_debug = debug;
		}
		
		public function get fullscreen() : Boolean {
			return _fullscreen;
		}
		
		public function set fullscreen(fullscreen : Boolean) : void {
			_fullscreen = fullscreen;
			fullscreen ? stage.displayState = StageDisplayState.FULL_SCREEN : stage.displayState = StageDisplayState.NORMAL;
		}
		
		public function get scalable() : Boolean {
			return _scalable;
		}
		
		public function set scalable(scalable : Boolean) : void {
			_scalable = scalable;
			if(!scalable){
			
				stage.scaleMode = StageScaleMode.SHOW_ALL;
				stage.align = StageAlign.TOP_LEFT;
				stage.removeEventListener(Event.RESIZE, onResize);
				
			} else {
		
				stage.scaleMode = StageScaleMode.NO_SCALE;
				stage.align = StageAlign.TOP_LEFT;
				stage.addEventListener(Event.RESIZE, onResize);
			
			}
		}
	
		protected function config(configUrl:String = null):void {
		
			// Check if a settings XML URL was passed through with flash vars
			var url:String;
			loaderInfo.parameters.configUrl ? url = loaderInfo.parameters.configUrl : url = configUrl;
			// If a hardwired settings XML URL is passed through and it's allready being set by flash vars then it will be overridden
			if(url) {
				
				ConfigManager.getInstance().addEventListener(LWEventTypes.ON_COMPLETE, onConfigLoadComplete);
				ConfigManager.getInstance().load(url);
			}
		}
		
		protected function init():void{
		
		}
		
		private function configure():void{
		
			for (var i : int = 0; i < settings.length(); i++) {
				switch (String(settings[i].@id)){
					case "scalable":
						scalable = Boolean(settings[i]);
						break;
					case "loop":
						if(settings[i].@useFrames) LoopManager.getInstance().useFrames = Boolean(settings[i].@useFrames);
						if(settings[i].@interval) LoopManager.getInstance().interval = Number(settings[i].@interval);
						if(settings[i].@autoStart) LoopManager.getInstance().start();
							LoopManager.getInstance().addEventListener(LWEventTypes.ON_LOOP, onLoop);
						break;
				}
			}
		}
		
		protected function addListeners():void{
			EventBroadcaster.addEventListener(LWEventTypes.SCALABLE, onScalableStatus);
			
			EventBroadcaster.addEventListener(LWEventTypes.FULLSCREEN, onFullScreenStatus);
			LoopManager.getInstance().addEventListener(LWEventTypes.ON_LOOP, onLoop);
		}
		
		// EVENT Handlers
		private function onConfigLoadComplete(event:Event):void{
			ConfigManager.getInstance().removeEventListener(LWEventTypes.ON_COMPLETE, onConfigLoadComplete);
			var assets:XMLList = ConfigManager.getInstance().getAssets();
			settings = ConfigManager.getInstance().getSettings();
			if(settings.length()>0) configure();
			if(assets.length()>0){
				InitLoadManager.getInstance().addEventListener(LWEventTypes.ON_COMPLETE, onAssetsLoadComplete);
				InitLoadManager.getInstance().load(assets)  
			} else {
				init();
			}
		}
		
		private function onAssetsLoadComplete(event:Event):void{
			InitLoadManager.getInstance().removeEventListener(LWEventTypes.ON_COMPLETE, onAssetsLoadComplete);
			init();
		}
		
		private function onScalableStatus ( event:RequestEvent ):void{
			scalable = event.params.status;
		}
		
		private function onFullScreenStatus ( event:RequestEvent ):void{
			fullscreen = event.params.status;
		}

		private function onLoopStatus ( event:RequestEvent ):void{
			event.params.status ? LoopManager.getInstance().start() : LoopManager.getInstance().stop();
			if(event.params.settings) LoopManager.getInstance().settings(event.params.settings);
		}
		
		protected function onLoop( event : Event ) : void {
			EventBroadcaster.dispatchEvent(new Event(LWEventTypes.ON_LOOP));
		}
		
		protected function onResize( event : Event ):void{
			EventBroadcaster.dispatchEvent(new Event(LWEventTypes.ON_RESIZE));
		
	
		}
		
	}
}
